//
//  CYPopSelectView.m
//  CYPopSelectView
//
//  Created by 楼楚 on 16/6/2.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "CYPopSelectView.h"

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height


@interface CYPopSelectView ()<UITableViewDelegate, UITableViewDataSource>
//origin of button
@property (nonatomic, assign) CGPoint origin;
//height of pop view
@property (nonatomic, assign) CGFloat height;
//width of pop view
@property (nonatomic, assign) CGFloat width;
//
@property (nonatomic, assign) CYDirectionType type;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation CYPopSelectView

- (instancetype)initWithOrigin:(CGPoint)origin Width:(CGFloat)width Height:(CGFloat)height Type:(CYDirectionType)type Color:(UIColor *)color{
    
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    if (self) {
        self.origin = origin;
        self.width = width;
        self.height = height;
        self.type = type;
        self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(origin.x, origin.y, width, height)];
        self.backgroundView.backgroundColor = color;
        [self addSubview:self.backgroundView];
        [self.backgroundView addSubview:self.tableView];
        
    }
    
    return self;
}
#pragma mark - draw popView
- (void)popView
{
    NSArray *subViews = [self.backgroundView subviews];
    for (UIView *view in subViews) {
        [view setHidden:YES];
    }
    UIWindow *windowView = [UIApplication sharedApplication].keyWindow;
    [windowView addSubview:self];
    switch (self.type) {
        case CYTypeOfUpLeft:     // 上左❶
        {
            self.backgroundView.frame = CGRectMake(self.origin.x, self.origin.y, 0, 0);
            [self startAnimationViewx:self.origin.x - self.width y:self.origin.y - self.height width:self.width height:self.height];
        }
            break;
            
        case CYTypeOfUpCenter:   // 上中❷
        {
            self.backgroundView.frame = CGRectMake(self.origin.x , self.origin.y, 0, 0);
            [self startAnimationViewx:self.origin.x - self.width*0.5 y:self.origin.y - self.height width:self.width height:self.height];
        }
            break;
            
        case CYTypeOfUpRight:    // 上右❸
        {
            self.backgroundView.frame = CGRectMake(self.origin.x , self.origin.y, 0, 0);
            [self startAnimationViewx:self.origin.x + self.width y:self.origin.y - self.height width:self.width height:self.height];
            
        }
            break;
            
        case  CYTypeOfLeft:     // 左❹
        {
            self.backgroundView.frame = CGRectMake(self.origin.x , self.origin.y, 0, 0);
            [self startAnimationViewx:self.origin.x - self.width y:self.origin.y - self.height*0.5 width:self.width height:self.height];
            
        }
            break;
            
        case CYTypeOfRight:    // 右❺
        {
            self.backgroundView.frame = CGRectMake(self.origin.x , self.origin.y, 0, 0);
            [self startAnimationViewx:self.origin.x + self.width y:self.origin.y - self.height*0.5 width:self.width height:self.height];
            
        }
            break;
            
        case CYTypeOfDownLeft:   // 下左❻
        {
            self.backgroundView.frame = CGRectMake(self.origin.x , self.origin.y, 0, 0);
            [self startAnimationViewx:self.origin.x - self.width y:self.origin.y + self.height width:self.width height:self.height];
            
        }
            break;
            
        case CYTypeOfDownCenter: // 下中❼
        {
            self.backgroundView.frame = CGRectMake(self.origin.x , self.origin.y, 0, 0);
            [self startAnimationViewx:self.origin.x - self.width*0.5 y:self.origin.y + self.height width:self.width height:self.height];
            
        }
            break;
            
        case CYTypeOfDownRight:  // 下右❽
        {
            self.backgroundView.frame = CGRectMake(self.origin.x , self.origin.y, 0, 0);
            [self startAnimationViewx:self.origin.x + self.width y:self.origin.y + self.height width:self.width height:self.height];
        }
            break;
        default:
            break;
    }
}
- (void)startAnimationViewx:(CGFloat)x y:(CGFloat)y width:(CGFloat)width height:(CGFloat)height{
    [UIView animateWithDuration:0.2 animations:^{
        self.backgroundView.frame = CGRectMake(x, y, width, height);
    } completion:^(BOOL finished) {
        NSArray *subViews = [self.backgroundView subviews];
        for (UIView *view in subViews) {
            [view setHidden:NO];
        }
    }];
    
}

#pragma mark -touches->dismiss
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (![[touches anyObject].view isEqual:self.backgroundView]) {
        [self dismiss];
    }
    
}

#pragma mark - dismiss subViews
- (void)dismiss{
    NSArray *subViews = [self.backgroundView subviews];
    for (UIView *view in subViews) {
        [view removeFromSuperview];
    }
    [UIView animateWithDuration:0.2 animations:^{
        self.backgroundView.frame = CGRectMake(self.origin.x, self.origin.x, 0, 0);
    } completion:^(BOOL finished) {
//        since completed,remove the popView from its superview
        [self removeFromSuperview];
    }];
}

#pragma mark - build tableView
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.backgroundView.frame.size.width, self.backgroundView.frame.size.height) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
    }
    return _tableView;
}

#pragma mark - tableView datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.row_height == 0) {
        return 40;
    }else{
        return  self.row_height;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, self.row_height*0.8, self.row_height*0.8)];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
    [cell.contentView addSubview:imageView];
    cell.backgroundColor = [UIColor clearColor];
    
    UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(5 + self.row_height, 5, self.width - self.row_height, self.row_height *0.9)];
    textLabel.text = self.titleArray[indexPath.row];
    textLabel.font = [UIFont systemFontOfSize:self.fontSize];
    textLabel.textColor = self.titleColor;
    [cell.contentView addSubview:textLabel];

    return cell;
}
// 想要实现点击进行其他操作, 这里用到了协议
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectIndexPathRow:)]) {
        [self.delegate selectIndexPathRow:indexPath.row];
    }
}
@end
